package com.younion.common.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;

import com.younion.common.constants.SysConstants;

public class ImageFileUtil {
	
	public static int getImgWidth(File file) {
    	InputStream is = null;
    	BufferedImage src = null;
    	int width = -1;
    	try {
    		is = new FileInputStream(file);
    		src = javax.imageio.ImageIO.read(is);
    		width = src.getWidth();
    		is.close();
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return width;
    }
    
    public static int getImgHeight(File file) {
    	InputStream is = null;
    	BufferedImage src = null;
    	int height = -1;
    	try {
    		is = new FileInputStream(file);
    		src = javax.imageio.ImageIO.read(is);
    		height = src.getHeight();
    		is.close();
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return height;
    }
    
    /**
     * 拷贝文件并删除旧文件
     * @param fromFile 源文件夹
     * @param toFile 目标文件夹
     * @param fileNames 图片名称字符串（以逗号相隔）
     */
    public static void copyImage(File fromFile, File toFile, String fileNames) {
    	if (fileNames == null || "".equals(fileNames))
    		return;
    	String fileName[] = fileNames.split(",");
    	if (!fromFile.exists())
    		fromFile.mkdirs();
    	if (!toFile.exists())
    		toFile.mkdirs();
    	File newFile;
    	File oldFile;
    	for (String name : fileName) {
    		newFile = new File(toFile, name);
    		oldFile = new File(fromFile, name);
    		try {
    			if (oldFile.exists()) {
    				FileUtils.copyFile(oldFile, newFile);
    				oldFile.delete();
    			}
    		} catch (IOException ioe) {
    			ioe.printStackTrace();
    		}
    	}
    }
    
    public static void deleteImage(File directory, String fileNames, boolean delDirectory) {
    	if (fileNames == null || "".equals(fileNames))
    		return;
    	File file;
    	String fileName[] = fileNames.split(",");
    	for (String name : fileName) {
    		if (name == null || name.trim().equals(""))
    			continue;
    		file = new File(directory, name);
    		if (file.exists())
    			file.delete();
    	}
    	if (delDirectory)
    		directory.delete();
    }
    
    public static void processImageFiles(int id, String suffixs, HttpServletRequest request) {
    	if (suffixs == null || suffixs.equals(""))
    		new ImageFileUtil().processImageFile(id, request.getParameter(SysConstants.IMG_ALL_PIC_STR_PARAMTER_NAME), request);
    	else {
    		String suffix[] = suffixs.split(",");
    		for (String s : suffix) {
    			new ImageFileUtil().processImageFile(id, request.getParameter(SysConstants.IMG_ALL_PIC_STR_PARAMTER_NAME + s), request);
    		}
    	}
    }
    
    private void processImageFile(int id, String picAllStr, HttpServletRequest request) {
    	String folder = picAllStr.split("FG")[0];
    	String removeFilesStr[] = picAllStr.split("FG")[1].split(",");
    	String filesStr[] = picAllStr.split("FG")[2].split(",");
    	String realPath = request.getSession().getServletContext().getRealPath(SysConstants.IMG_PATH);
    	File tempFile = new File(realPath + File.separator + folder + File.separator + "temp");
    	File realFile = new File(realPath + File.separator + folder + File.separator + id);
    	if (!realFile.exists())
    		realFile.mkdirs();
    	File file;
    	for (String fileName : removeFilesStr) {
    		file = new File(tempFile, fileName);
    		if (file.exists()) {
    			file.delete();
    		} else {
    			file = new File(realFile, fileName);
    			if (file.exists()) {
    				file.delete();
    			}
    		}
    	}
    	File oldFile;
    	for (String fileName : filesStr) {
    		file = new File(realFile, fileName);
    		if (!file.exists()) {
    			oldFile = new File(tempFile, fileName);
    			try {
    				if (oldFile.exists()) {
    					FileUtils.copyFile(oldFile, file);
    					oldFile.delete();
    				}
				} catch (IOException e) {
					e.printStackTrace();
				}
    			
    		}
    	}
    }

}
